﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.TextMining.Properties;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class PdfScanComponent:WorkflowComponentBase
    {
        #region props
        private PdfScanInstruction _Instruction;
        /// <summary>an object that encapsulate instruction settings.</summary>
        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction=(PdfScanInstruction)value; }
        }
      
        private PdfScanOutput _Output;
        /// <summary>an object to encapsulate output</summary>
        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output =(PdfScanOutput)value; }
        }

        /// <summary></summary>
        public string FileListInputXmlFilePath
        {
            get
            {
                return this._Instruction.FileListInputXmlFilePath;
            }
            set
            {
                this._Instruction.FileListInputXmlFilePath = value;
            }
        }

        /// <summary></summary>
        public string Pattern
        {
            get
            {
                return this._Instruction.Pattern;
            }
            set
            {
                this._Instruction.Pattern = value;
            }
        }

        /// <summary></summary>
        public double MatchScoreThreshold
        {
            get
            {
                return this._Instruction.MatchScoreThreshold;
            }
            set
            {
                this._Instruction.MatchScoreThreshold = value;
            }
        }

        /// <summary></summary>
        public int ThreadPoolSize
        {
            get
            {
                return this._Instruction.ThreadPoolSize;
            }
            set
            {
                this._Instruction.ThreadPoolSize = value;
            }
        }

        /// <summary></summary>
        public string LogFilePath
        {
            get
            {
                return this._Instruction.LogFilePath;
            }
            set
            {
                this._Instruction.LogFilePath = value;
            }
        }

        /// <summary></summary>
        public string ErrorFilePath
        {
            get
            {
                return this._Instruction.ErrorFilePath;
            }
            set
            {
                this._Instruction.ErrorFilePath = value;
            }
        }

        /// <summary>
        /// <Files>
        ///     <File DocID="1" Pages="1,2,3" IsElectronic="False" IsScanned="False" />
        /// </Files>
        /// </summary>
        public string ResultFilePath
        {
            get
            {
                return this._Instruction.ResultFilePath;
            }
            set
            {
                this._Instruction.ResultFilePath = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public PdfScanComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.Enumeration.ToString();
            this.ComponentName = "Pdf Scanner";
            this.Description = "Scan pdf files for string pattern";
            this.ComponentIcon = Resources.adobe;
            this.ComponentImage = Resources.Form;

            this._Instruction =
                new PdfScanInstruction(
                    this.ComponentID, Guid.Empty, string.Empty,
                    string.Empty, 0.8, 10, string.Empty,
                    string.Empty, string.Empty);
            this._Output = new PdfScanOutput(
                this.ComponentID, Guid.Empty, new List<string>(), string.Empty);
        }
        #endregion

        /// <summary><para>subclass must implement this method to instantiate activity from xml</para></summary>
        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                PdfScanComponent component = base.Instantiate(xNode) as PdfScanComponent;

                component.FileListInputXmlFilePath = XmlDataUtil.GetAttributeValue(xNode, "FileListInputXmlFilePath", "");
                component.Pattern = XmlDataUtil.GetAttributeValue(xNode, "Pattern", "");
                component.MatchScoreThreshold =
                    double.Parse(XmlDataUtil.GetAttributeValue(xNode, "MatchScoreThreshold", "0.8"));
                component.ThreadPoolSize = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "ThreadPoolSize", "10"));
                component.LogFilePath = XmlDataUtil.GetAttributeValue(xNode, "LogFilePath", "");
                component.ErrorFilePath = XmlDataUtil.GetAttributeValue(xNode, "ErrFilePath", "");
                component.ResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "ResultFilePath", "");

                return component;
            }
            else
            {
                throw new Exception("Unable to instantiate email: invalid type specified in xml node");
            }
        }

        /// <summary><para>must implementable method to serialize activity settings to xml</para></summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileListInputXmlFilePath", this.FileListInputXmlFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Pattern", this.Pattern);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "MatchScoreThreshold", this.MatchScoreThreshold.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ThreadPoolSize", this.ThreadPoolSize.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LogFilePath", this.LogFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ErrFilePath", this.ErrorFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ResultFilePath", this.ResultFilePath);
            
            return xNode;
        }
    }
}
